import { defineStore } from "pinia";
import { SystemMenuVO } from "@/entiy/vo/SystemMenuVO.ts";
import { VxeTablePropTypes } from "vxe-table";
import PromiseUtils from "@/utils/PromiseUtils.ts";
import RequestService from "@/service/RequestService.ts";
import { useCompanyInfoManagerStore } from "@/store/UseCompanyInfoManagerSotre.ts";
import XEUtils from "xe-utils";

export const useCompanyMenuManagerStore = defineStore("useCompanyMenuManagerStore", {
    state: () => {
        return {
            // 页面的加载状态
            isPageLoading: false,

            // 表格的加载状态
            isTableLoading: false,

            // 表格数据
            tableDataList: [] as SystemMenuVO[],

            // 行配置
            rowConfig: {
                isCurrent: true,
                isHover: true,
                useKey: true,
                keyField: "id"
            } as VxeTablePropTypes.RowConfig,

            // 树形表格配置
            treeConfig: {
                // 平级数据展开菜单
                transform: true,
                // 展示连接线
                showLine: true,
                // 展开全部行
                expandAll: true,
                // 展开指定行 expandRowKeys: [10053]
                // 是否使用手风琴效果，只能展开一个节点
                accordion: false,
                // 树节点 ID 字段名
                rowField: 'menuId',
                // 树节点父 ID 字段名
                parentField: 'menuParentId'
            } as VxeTablePropTypes.TreeConfig,

            // 表格编辑配置
            editConfig: {
                // 手动模式编辑
                trigger: "manual",
                // 按行模式编辑
                mode: "row",
                // 显示是否已经编辑的状态
                showStatus: false,
                // 自动清除编辑状态
                autoClear: true
            } as VxeTablePropTypes.EditConfig,

            // 当前的机构 ID
            currentOrgId: "",
        }
    },

    actions: {

        /**
         * 加载机构的菜单信息
         */
        async loadCompanyMenuData(currentOrgId: string): Promise<void> {
            if (XEUtils.isNull(currentOrgId) || XEUtils.isEmpty(currentOrgId)) {
                return Promise.resolve()
            }

            this.currentOrgId = currentOrgId;
            const url = "/get-org-menu-list/" + currentOrgId;
            const result = await RequestService.get<Array<SystemMenuVO>>(url);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },


        /**
         * 更新菜单信息
         */
        async updateCompanyMenuData(row: SystemMenuVO): Promise<void> {
            const body = {
                orgId: row.orgId,
                menuId: row.menuId,
                menuParentId: row.menuParentId,
                showStatus: row.showStatus
            }

            const result = await RequestService.post<Array<SystemMenuVO>>("/update-org_menu", body);
            if (result.isError) {
                return PromiseUtils.reject(result.errorMsg);
            }

            this.tableDataList = result.bizDta;
            return PromiseUtils.resolve();
        },

        /**
         * 设置监听器
         */
        async setupListener() {
            const companyInfoManagerStore = useCompanyInfoManagerStore();
            companyInfoManagerStore.$subscribe((_mutation, state) => {
                const selectedOrgId = state.currentOrgData.orgId ?? "";
                // 机构 ID 变化时重新加载数据
                if (this.currentOrgId !== selectedOrgId) {
                    this.currentOrgId = selectedOrgId;
                    this.loadCompanyMenuData(selectedOrgId);
                }
            });
        },
    }
})
